<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use think\Controller;
//header("Content-Type: text/html;charset=utf-8");
class Student extends Common
{

    /**
     * yang yang
     * 学生档案
     * @throws
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function index(){
        if (request()->isAjax()) {

            $page =input('page',1);
            $pageSize =input('limit',config('pageSize'));
            $keyword = input('post.keyword');

            $list = Db::table('jxjl_student')
                ->where('school_id', $this->school_id)
                ->where('true_name|student_number', 'like', "%" . $keyword . "%")
                ->page($page,$pageSize)
                ->order('add_time desc')
                ->select();


            if ($list) {
                foreach ($list as $k => $v) {
                    $list[$k]['add_time'] = toDate($v['add_time']);
                    $status=Db::name('hostel')->where('student_number',$v['student_number'])->find();
                    if($status){
                        $list[$k]['hostel']=$status['status'];
                    }else{
                        $list[$k]['hostel']=2;
                    }
                }
            }
            $total = Db::table('jxjl_student')
                ->where('school_id', $this->school_id)
                ->where('true_name|student_number', 'like', "%" . $keyword . "%")
                ->count();
            return $result = ['code'=>0,'msg'=>'成功!','data'=>$list,'count'=>$total,'rel'=>1];
        }
        return $this->fetch();
    }


    /**
     * yang yang
     * 是否外宿
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function editState(){
        $id = input('post.id');
        $status = input('post.status');
        $sudent = Db::table('jxjl_student')->where('id',$id)->find();
        $hostel = Db::name('hostel')->where('student_number',$sudent['student_number'])->find();
        if($hostel){
            $rr = Db::name('hostel')->where('student_number',$sudent['student_number'])->update(['status'=>$status]);
            if($rr){
                return ['code'=>1,'msg'=>'设置成功!'];
            }else{
                return ['code'=>0,'msg'=>'设置失败!'];
            }
        }else{
            $rr = Db::name('hostel')->insertGetId([
                'student_name'=>$sudent['true_name'],
                'student_number'=>$sudent['student_number'],
                'student_sn'=>$sudent['student_sn'],
                'school_id'=>$sudent['school_id'],
                'school_name'=>$sudent['school_name'],
                'grade_id'=>$sudent['grade_id'],
                'grade_name'=>$sudent['grade_name'],
                'class_id'=>$sudent['class_id'],
                'class_name'=>$sudent['class_name'],
                'mobile'=>$sudent['mobile'],
                'add_parentname'=>$sudent['add_parentname'],
                'school_code'=>$sudent['school_code'],
                'status'=>$status,
                'add_time'=>time(),
            ]);
            if($rr){
                return ['code'=>1,'msg'=>'设置成功!'];
            }else{
                return ['code'=>0,'msg'=>'设置失败!'];
            }
        }

    }
    /**
     * yang yang
     * 学生档案
     * @throws
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function hostel(){
        if (request()->isAjax()) {

            $page =input('page',1);
            $pageSize =input('limit',config('pageSize'));
            $keyword = input('post.keyword');

            $list = Db::name('hostel')
                ->where('status', 1)
                ->where('student_name|student_number', 'like', "%" . $keyword . "%")
                ->page($page,$pageSize)
                ->order('add_time desc')
                ->select();
            if (! empty($list)) {
                foreach ($list as $k => $v) {

                    $list[$k]['add_time'] = toDate($v['add_time']);

                }
            }
            $total = Db::name('hostel')
                ->where('status', 1)
                ->where('student_name|student_number', 'like', "%" . $keyword . "%")
                ->count();
            return $result = ['code'=>0,'msg'=>'成功!','data'=>$list,'count'=>$total,'rel'=>1];
        }
        return $this->fetch();
    }

}